/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.semi.fw;

import com.semi.util.FileLogger;

/**
 *
 * @author liaoyq
 */
public class UnCoderMgr {
    protected  FileLogger mLogger = new FileLogger(UnCoderMgr.class.getName());
    private UnCoder[] mData = new UnCoder[100];
    public UnCoderMgr(){
    }
    public int size(){ return mData.length; }
    public UnCoder get(int index){ return mData[index]; }
    public int length() {
        for(int i=0;i<mData.length;i++)
            if( mData[i]==null ) return i;
        return mData.length; 
    }
    public void clear(){
        mLogger.info("remove all");
        for(int i=mData.length-1;i>=0;i--) mData[i] = null;
    }
    /*
    public int findBySeq(String seq){
        for(int i=0;i<mData.length;i++){
            if( mData[i]== null ) return -1;
            if( mData[i].mSeq.compareTo(seq)==0 ) return i;
        }
        return -1;
    } */   
    public boolean add(UnCoder coder){
        for(int i=0;i<mData.length;i++){
            if( mData[i]== null ){
                mData[i]=coder;
                mLogger.info("add to index="+i+", "+coder.dump());                
                return true;
            }
            if( mData[i].validate() ) continue;
            mData[i] = coder;
            mLogger.info("add to index="+i+", "+coder.dump());            
            return true;
        }
        return false;
    }
    public void remove(int index){
        mLogger.info("remove rule index="+index);        
        if( mData[index]!=null ) mData[index].mEnable = false;
    }
    public int match(String s){
        if( s== null ) return -1;
        for(int i=0;i<mData.length;i++){
            if( mData[i]!=null ){
                if( mData[i].match(s) ) return i;
            }
        }
        return -1;
    }
}
